//
//  totalTableController.swift
//  account
//
//  Created by Qingke on 16/4/26.
//  Copyright © 2016年 Qingke. All rights reserved.
//

import UIKit
import CoreData
class totalTableController: UITableViewController {

    var results = []
    var appDel: AppDelegate!
    var context: NSManagedObjectContext!
    override func viewDidLoad() {
        super.viewDidLoad()
        appDel = UIApplication.sharedApplication().delegate as! AppDelegate
        context = appDel.managedObjectContext
        loaddata()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source


    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return results.count
    }

    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("coustCell", forIndexPath: indexPath)
        let onetip = results[indexPath.row] as! NSManagedObject
        cell.detailTextLabel?.text = String (onetip.valueForKey("number") as? Double ?? 0.0)
        cell.textLabel?.text = onetip.valueForKey("item") as? String ?? "meiyou"
        return cell
    }
    
    @IBAction func back(sender: UIBarButtonItem) {
        
        dismissViewControllerAnimated(true, completion: nil)
    }
    

  
}
extension totalTableController {
    func loaddata() {
        let request = NSFetchRequest(entityName: "Account")
        request.returnsObjectsAsFaults = false
        let result1 = try! context.executeFetchRequest(request)
        print("加载")
        //*******这里显示的是按照顺序进行的，所以需倒叙排序*****
        results = result1.reverse()
    }
    
}